package cn.config;

import com.ymx.darling.bean.entity.dynamic.DynamicSchemaProcessor;
import com.ymx.darling.bean.entity.dynamic.DynamicTableProcessor;
import com.ymx.darling.intercept.*;
import com.ymx.darling.logicdelete.LogicDeleteProcessor;
import com.ymx.darling.spring.boot.autoconfigure.DarlingCustomizer;
import com.ymx.darling.tenant.TenantFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @author 爱Java的小于
 */
@Component
public class MyConfig implements DarlingCustomizer {
    @Override
    public WrapperInterceptor getWrapperInterceptor() {
        return null;
    }

    @Override
    public TenantFactory getTenantFactory() {
        return null;
    }

    @Override
    public Map<String, Function<Class<?>, ?>> getFillValueMap() {
        Map<String, Function<Class<?>, ?>> fillMap = new HashMap<>();
        fillMap.put("sex", clazz -> "男");
        return fillMap;
    }

    @Override
    public Map<Class<?>, LogicDeleteProcessor> getLogicDeleteProcessorMap() {
        return null;
    }

    @Override
    public ExecutorInterceptor getExecutorInterceptor() {
        return new MyExecutorInterceptor();
    }

    @Override
    public DynamicTableProcessor getDynamicTableProcessor() {
        return null;
    }

    @Override
    public DynamicSchemaProcessor getDynamicSchemaProcessor() {
        return null;
    }


    @ExecutorInterceptMethods({"update", "queryList"})
    private static class MyExecutorInterceptor implements ExecutorInterceptor {
        @Override
        public Object intercept(Invocation invocation) throws Throwable {
            long start = System.currentTimeMillis();
            Object result = invocation.proceed();
            System.out.println("方法:"+invocation.getMethodName()+"查询总计耗时:"+ (System.currentTimeMillis() - start));
            return result;
        }
    }
}
